package com.cartrentalsystem.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cartrentalsystem.domain.po.Rental;
import com.cartrentalsystem.domain.vo.RentalVo;
import com.cartrentalsystem.domain.vo.Result;
import com.cartrentalsystem.service.IRentalService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author LHD
 * @since 2025-06-15
 */
@RestController
@RequestMapping("/rental")
@RequiredArgsConstructor
@Slf4j
public class RentalController {

    private final IRentalService rentalService;

    @GetMapping("/list")
    public Result<Page<RentalVo>> list(@RequestParam(name = "page",defaultValue = "1") Integer page,
                                       @RequestParam(name = "size",defaultValue = "10") Integer size,
                                       @RequestParam(name = "customerName",required = false) String customerName,
                                       @RequestParam(name = "plateNumber",required = false) String plateNumber,
                                       @RequestParam(name = "status",required = false) Integer status) {
        Result<Page<RentalVo>> list = rentalService.list(page, size, customerName, plateNumber, status);
        return list;
    }

    // 管理员直接修改的信息（可能会导致数据不一致）【强制性的】
    @PutMapping("/update")
    public Result<String> update(@RequestBody RentalVo rentalVo) {
        Result<String> update = rentalService.updateRental(rentalVo);
        return update;
    }

    @DeleteMapping("/delete/{id}")
    public Result<String> delete(@PathVariable Integer id) {
        log.info("id: {}", id);
        Result<String> delete = rentalService.deleteRental(id);
        log.info("delete: {}", delete);
        return delete;
    }

    /**
     * 处理租车，租赁status依旧是1，但是会有归还日期，说明在维修中
     */
    // 处理还车（包括了正常和要维修的情况）
    @PutMapping("/updateRentalStatus/{id}")
    public Result<String> updateRentalStatus(@PathVariable("id") Integer id, @Param("status") Integer status) {
        log.info("id: {}, status: {}", id, status);
        Result<String> updateRentalStatus = rentalService.updateRentalStatus(id, status);
        log.info("updateRentalStatus: {}", updateRentalStatus);
        return updateRentalStatus;
    }

    @PostMapping("/add")
    public Result<String> add(@RequestBody RentalVo rentalVo) {
        log.info("rentalVo: {}", rentalVo);
        Result<String> add = rentalService.addRental(rentalVo);
        log.info("add: {}", add);
        return add;
    }

    @GetMapping("/customer/{customerId}")
    public Result<List<RentalVo>> getRentalByCustomerId(@PathVariable("customerId") Integer customerId) {
        log.info("customerId: {}", customerId);
        List<RentalVo> rentals = rentalService.getRentalByCustomerId(customerId);
        log.info("rental: {}", rentals);
        return Result.success(rentals);
    }

}
